from django.db import models
from django.contrib.auth.models import AbstractUser,BaseUserManager,AbstractBaseUser, PermissionsMixin
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.contrib.auth import get_user_model

# # Create your models here.
# class Person(User):
#     class Meta:
#         proxy = True #person就是User的一个代理
#
#     @classmethod
#     def get_blacklist(cls): #参数就是User
#         return cls.objects.filter(is_active=False)


# class UserExtension(models.Model):
# #     user = models.OneToOneField(User,on_delete=models.CASCADE,related_name="extension")
# #     telephone = models.CharField(max_length=11)
# #     school = models.CharField(max_length=50)
# #
# # @receiver(post_save,sender=User)
# # def handles_user_extension(sender,instance,created,**kwargs):
# #     if created:
# #         UserExtension.objects.create(user=instance)
# #         #这是新建
# #     else:
# #         instance.extension.save() #这是修改

class  UserManager(BaseUserManager):
    def _create_user(self, username, telephone, password, **kwargs):
        if not username:
            raise ValueError('用户名必须写')
        if not telephone:
            raise ValueError('手机号必须写')
        if not password:
            raise ValueError('密码必须写')
        user = self.model(telephone=telephone,username=username, password=password, **kwargs)
        user.set_password(password)
        user.save()
        return user

    def create_user(self, telephone,username,password=None, **kwargs):
        kwargs['is_superuser']=False
        return self._create_user(telephone=telephone,username=username, password=password, **kwargs)


    def create_superuser(self, telephone,username,password=None, **kwargs):
        kwargs['is_superuser'] = True
        return self._create_user(telephone=telephone, username=username, password=password, **kwargs)

# class User(AbstractUser):
#     telephone = models.CharField(max_length=11,unique=True)
#     school = models.CharField(max_length=50)
#
#     USERNAME_FIELD = 'telephone'
#     objects = UserManager()

class User(AbstractBaseUser,PermissionsMixin):
    telephone = models.CharField(max_length=11,unique=True)
    email = models.EmailField(max_length=100,unique=True)
    username = models.CharField(max_length=100,unique=True)
    is_active = models.BooleanField(default=True)

    USERNAME_FIELD = 'telephone'
    REQUIRED_FIELDS = []


    objects = UserManager()

    def get_full_name(self):
        return self.username

    def get_short_name(self):
        return self.username


class Article(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    author = models.ForeignKey(get_user_model(),on_delete=models.CASCADE)

    class Meta:
        permissions = [
            ("view_article","查看文章的权限")
        ]



